package cn.car.controller;

import cn.car.biz.SysUserBiz;
import cn.car.domains.vo.BasePage;
import cn.car.domains.vo.Result;
import cn.car.domains.vo.sysUser.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/user")
@Tag(name = "sysUserController",description = "用户管理相关接口")
public class SysUserController {


    @Resource
    private SysUserBiz sysUserBiz;



    /**
     * 创建用户
     *
     * @param req 创建用户请求参数
     */
    @PostMapping("/create")
    @Operation(description = "创建用户接口：根据请求参数创建一个新的系统用户")
    public Result<Void> createUser(@RequestBody @Valid CreateUserReq req) {
        sysUserBiz.createUser(req);
        return Result.success();
    }

    /**
     * 根据ID获取用户信息
     *
     * @param id 用户ID
     */
    @GetMapping("/getById")
    @Operation(description = "根据用户ID查询用户信息，返回用户详情")
    public Result<SysUserRes> getUserById(@Parameter(
            schema = @Schema(name = "id", description = "用户id")) @RequestParam  Long id) {
        return Result.success(sysUserBiz.getUserById(id));
    }

    /**
     * 根据用户名获取用户信息
     *
     * @param username 用户名
     */
    @PostMapping("/getByUsername")
    @Operation(description = "根据用户名查询用户信息，用于前端按用户名检索用户")
    public Result<SysUserRes> getUserByUsername(@Parameter(
            schema = @Schema(name = "username", description = "用户名")) @RequestParam String username) {
        return Result.success(sysUserBiz.getUserByUsername(username));
    }

    /**
     * 更新用户信息
     *
     * @param req 更新用户请求参数
     */
    @PostMapping("/update")
    @Operation(description = "更新用户信息接口��接收用户更新参数并保存变更")
    public Result<Void> updateUser(@RequestBody @Valid UpdateUserReq req) {
        sysUserBiz.updateUser(req);
        return Result.success();
    }

    /**
     * 删除用户
     *
     * @param id 用户ID
     */
    @PostMapping("/delete")
    @Operation(description = "删除用户接口：根据用户ID删除对应用户记录")
    public Result<Void> deleteUser(@Parameter(
            schema = @Schema(name = "id", description = "用户id")) @RequestParam Long id) {
        sysUserBiz.deleteUser(id);
        return Result.success();
    }


    @PostMapping("/login")
    @Operation(description = "用户登录接口：根据用户名和密码进行认证并返回用户信息")
    public Result<SysUserRes> login(@RequestBody @Valid UserLoginReq req) {
        return Result.success(sysUserBiz.login(req));
    }

    /**
     * 分页查询用户信息
     */
    @PostMapping("/list")
    @Operation(description = "分页查询用户信息接口：根据分页参数返回用户列表，支持按用户名模糊查询")
    public Result<BasePage<SysUserRes>> list(@RequestBody UserListReq req) {
        BasePage<SysUserRes> pageInfo = sysUserBiz.list(req);
        return Result.success(pageInfo);
    }

    @PostMapping("/addUserRole")
    @Operation(summary = "添加用户角色",description = "添加用户角色")
    public Result<Void> addUserRole(@Valid @RequestBody AddUserRoleReq req){
        sysUserBiz.addUserRole(req);
        return Result.success();
    }

}
